package com.youhp.spire.xls.demo.Charts.ManipulateChart;

import com.spire.xls.*;

public class ValueFromCells {

    public static void main(String[] args)
    {
        //Create a workbook
        Workbook workbook = new Workbook();
        workbook.setVersion(ExcelVersion.Version2013);

        //Get the first sheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Set chart data
        CreateChartData(sheet);

        //Add a chart
        Chart chart = sheet.getCharts().add(ExcelChartType.ColumnClustered);
        chart.setDataRange(sheet.getCellRange("A1:B4"));
        chart.setSeriesDataFromRange(false);
        chart.setTopRow(5);
        chart.setLeftColumn(2);
        chart.setWidth(500);
        chart.setHeight(300);

        //Define the value
        chart.getSeries().get(0).getDataPoints().getDefaultDataPoint().getDataLabels().setValueFromCell(sheet.getCellRange("C1:C4"));

        chart.getPrimaryValueAxis().hasMajorGridLines(false);

        //Save the result file
        workbook.saveToFile("output/ValueFromCells.xlsx", FileFormat.Version2013);

    }
    private static  void CreateChartData(Worksheet sheet)
    {
        //Set value of specified range
        sheet.getCellRange("A1").setValue("Cuba");
        sheet.getCellRange("A2").setValue("German");
        sheet.getCellRange("A3").setValue("Mexico");
        sheet.getCellRange("A4").setValue("France");
        sheet.getCellRange("B1").setNumberValue(6000);
        sheet.getCellRange("B2").setNumberValue(8000);
        sheet.getCellRange("B3").setNumberValue(7600);
        sheet.getCellRange("B4").setNumberValue(8760);

        sheet.getCellRange("C1").setNumberValue(4350);
        sheet.getCellRange("C2").setNumberValue(6770);
        sheet.getCellRange("C3").setNumberValue(8700);
        sheet.getCellRange("C4").setNumberValue(7786);
    }
}
